#!/usr/bin/python
# Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
# Any live cell with more than three live neighbours dies, as if by overcrowding.
# Any live cell with two or three live neighbours lives on to the next generation.
# Any dead cell with exactly three live neighbours becomes a live cell.


from time import sleep
import sys
from random import randint

from game_of_life_unstable_backend import GameOfLife

if sys.version_info < (3, 0):
    raise "must use python 3.0 or greater"
    
def start(l=[], c=0):
    while True:
        print("[B]omb\n[G]lider\n[P]ulsar\n[R]andom\n")
        x = input("Which preset?").lower()
        if x == 'b':
            return [(5,5),(6,6),(6,7),(5,8),(4,7),(4,6),(5,6)]
        if x == 'g':
            return [(5,5),(6,6),(6,7),(5,7),(4,7)]
        if x == 'p':
            return [(5, 5), (6, 5), (7, 5), (8, 5), (9, 5), (10, 5), (5, 6), (6, 6), (7, 6), (8, 6), (9, 6), (10, 6),
             (8, 12), (9, 12), (10, 12), (11, 12), (12, 12), (13, 12), (8, 13), (9, 13), (10, 13), (11, 13), (12, 13),
            (13, 13), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (6, 8), (6, 9), (6,10), (6, 11), (6, 12),
             (6, 13), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10),(13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10) ]
        while x == 'r':
            t = (randint(1,20),randint(1,20))
            l.append(t)
            c += 1
            if c > 50:
                return l
        else:
            continue

GoL = GameOfLife(start())

def show_field(): # this can be changed to dispaly whatever section of the board you want, stretching to infinity
    bookends = ("| " + (" - "*20)+ " |")
    print(bookends)
    for y in range(1,21):
        line = ""
        for x in range(1,21):
            if (x,y) in GoL.cells:
                line += " X "
            else:
                line = line + " . "
        print("| " + line + " |")
    print(bookends)

while True:
    show_field()
    GoL.step()
    choice = input("Enter to step. P to play")
    while choice == 'p':
        show_field()
        GoL.step()
        sleep(0.2)
        